package com.offer;


public class Test53_1 {

    public int search(int[] nums, int target) {
        if (nums.length <= 0) {
            return 0;
        }

        int count = 0;

        int left = 0, right = nums.length - 1;

        int mid = 0;

        while (left <= right) {
            mid = (right - left) / 2 + left;
            if (nums[mid] == target) {
                break;
            } else if (nums[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }

        int midUp = mid + 1;

        while (mid >= 0 && nums[mid] == target) {
            count++;
            mid--;
        }

        while (midUp < nums.length && nums[midUp] == target) {
            count++;
            midUp++;
        }

        return count;
    }

    public static void main(String[] args) {
        Test53_1 test = new Test53_1();
//        System.out.println(test.search(new int[]{5, 7, 7, 8, 8, 10}, 8));
        System.out.println(test.search(new int[]{1, 2, 3}, 1));
//        DataUtils.printListNode(test.reserveList(a));
    }

}
